package com.panshi.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.panshi.interceptor.GlobalInterceptor;
import com.panshi.interceptor.SMSInterceptor;
import com.panshi.tools.MyTimeTools;
import com.panshi.tools.ResponseData;
import com.panshi.tools.SMStools;

public class SMSController extends Controller {


    @ActionKey("/short-message")
    @Before({GlobalInterceptor.class, SMSInterceptor.class})
    public void short_message() {
        String mobile = getPara("mobile");//手机号
        String code = SMStools.genRandomMath(6);//验证码位数
        Record short_message = new Record();
        short_message.set("code", code).set("time", MyTimeTools.getUnixTimeMills()).set("type", 1).set("valid_time", 300)
                .set("mobile", mobile).set("user_id", 0).set("status", 0).set("packageId", getAttr("packageId"));
        Db.save("ot_app_short_message", short_message);
        String test = SMStools.send_vertified_code(code, mobile, "磐石APP测试");
        System.out.println(test);
        renderJson(ResponseData.ok());
    }

    /**
     * 短信验证码验证服务
     * @return
     */

        public  boolean short_message_check(String mobile,String code) {
        int count = Db.update("update ot_app_short_message set valid_time='" + MyTimeTools.getUnixTimeMills() + "' , `status`=1 where valid_time=300 and `status`=0 and mobile='" + mobile + "' and code='" + code + "'");
        return count>0?true:false;
    }

}
